package com.example.controller.buyer;

import com.example.base.BaseController;
import com.example.entity.FootPrint;
import com.example.vo.buyer.SpuSkuDetail;
import com.example.service.FootPrintService;
import com.example.vo.IdHolder;
import com.example.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author 朽
 */
@RestController
@Slf4j
@RequestMapping("/buyer/foot")
public class FootPrintController extends BaseController<FootPrintService, FootPrint> {

    @GetMapping("/footPrint")
    @Operation(summary = "获取用户足迹")
    public Result<List<SpuSkuDetail>> getFootPrint() {
        Long userId = IdHolder.getUserId();
        if (userId == null) {
            return Result.error("用户未登录或会话已过期");
        }
        List<SpuSkuDetail> details = baseService.getDetail(userId);
        return Result.ok(details);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除对应足迹")
    public Result<String> deleteFoot(@RequestParam Long id){
        Long userId = IdHolder.getUserId();
        boolean isDelete = baseService.deleteFoot(userId, id);
        if(isDelete){
            return Result.ok();
        }
        return Result.error("删除失败");
    }

    @DeleteMapping("/delAll")
    @Operation(summary = "清空足迹")
    public Result<String> deleteAllFoot(){
        Long userId = IdHolder.getUserId();
        boolean isDelete = baseService.deleteAllFoot(userId);
        if(isDelete){
            return Result.ok();
        }
        return Result.error("删除失败");
    }

    @PostMapping("/record")
    @Operation(summary = "添加足迹")
    public Result<String> recordFootPrint(@RequestParam Long spuId, @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date footTime) {
        Long userId = IdHolder.getUserId();
        baseService.addFootPrint(userId,spuId,footTime);
        return Result.ok();
    }

}
